package com.mayikt.member.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.constants.Constants;
import com.mayikt.core.util.GenerateTokenUtils;
import com.mayikt.member.input.dto.UserLoginInpDTO;
import com.mayikt.member.mapper.UserMapper;
import com.mayikt.member.mapper.entity.UserEntity;
import com.mayikt.member.service.QQAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Service
public class QQAuthorServiceImpl extends BaseApiService<JSONObject> implements QQAuthorService{
    @Autowired
    private GenerateTokenUtils generateTokenUtils;
    @Autowired
    private UserMapper userMapper;
    @Override
    public BaseResponse<JSONObject> getUserByOpenId(String openId) {
        if (StrUtil.isEmpty(openId)){
            return setResultError("openId不能为空!");
        }
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(UserEntity::getQqOpenId,openId);
        UserEntity userEntity = userMapper.selectOne(queryWrapper);
        if(null == userEntity){
            return setResultError(Constants.HTTP_RES_ERROR_CODE_203,"未查询到openId关联的账户！");
        }
        String prefix = Constants.MEMBER_TOKEN_KEYPREFIX + UserLoginInpDTO.LoginType.QQ.getLoginTypeStr();
        String token = generateTokenUtils.createToken(prefix, "" + userEntity.getId());
        JSONObject data = new JSONObject();
        data.put("token",token);
        return setResultSuccess(data);
    }
}
